home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <aux.h>
-
- #define auxMain
- #include <auxPrivate.h>
- #undef auxMain
-
-
- enum { NOT_SET, RGB_MODE, INDEX_MODE };
-
- /**************************************************************************
- * Window parameters used for window initialization
- **************************************************************************/
-
- struct _win {
- GLint x;
- GLint y;
- GLsizei width;
- GLsizei height;
- GLsizei aspectWidth;
- GLsizei aspectHeight;
- } win;
-
- /**************************************************************************
- * Global variables and flags
- **************************************************************************/
-
- GLint colormapMode = NOT_SET;
- GLboolean initPosition = GL_FALSE;
- GLboolean keepAspect = GL_FALSE;
-
- int attribList[MAX_ATTRIB] = { None };
-
-
- /**************************************************************************
- * auxInit() - Open connection to server and initialize state variables
- **************************************************************************/
-
- GLvoid
- auxInit( GLvoid )
- {
- GLint i;
- char *displayName = (char *) NULL;
- int erb, evb;
-
- if ( (displayName = getenv( "DISPLAY" )) == (char *) NULL )
- displayName = ":0";
-
- auxState.display = XOpenDisplay( displayName );
-
- if ( auxState.display == (Display *) NULL ) {
- char str[80];
-
- sprintf( str, "Unable to open display '%s'", displayName );
- auxFatalError( str );
- }
- if (!glXQueryExtension(auxState.display, &erb, &evb)) {
- auxFatalError("No glx extension!");
- }
-
-
- #ifdef DEBUG
- XSetErrorHandler( auxXErrorHandler );
- #endif
-
- if ( atexit( auxCleanup ) )
- auxFatalError( "Couldn't register auxCleanup " );
-
- auxState.screen = DefaultScreenOfDisplay( auxState.display );
- auxState.screenNum = DefaultScreen( auxState.display );
- auxState.rootWindow = RootWindow( auxState.display, auxState.screenNum );
-
- auxState.current = (auxWindow *) NULL;
- auxState.head = (auxWindow *) NULL;
- auxState.tail = (auxWindow *) NULL;
-
- auxState.rgbColormap = (Colormap) NULL;
- auxState.indexColormap = (Colormap) NULL;
-
- auxInitCalled = GL_TRUE;
- }
-
-
- /**************************************************************************
- * auxKeepAspect() - Initialize window attribute variables
- **************************************************************************/
-
- void
- auxKeepAspect( GLsizei width, GLsizei height )
- {
- if ( width <= ZERO )
- auxFatalError( "Window width <= 0" );
-
- if ( height <= ZERO )
- auxFatalError( "Window height <= 0" );
-
- win.aspectWidth = width;
- win.aspectHeight = height;
-
- keepAspect = GL_TRUE;
- }
-
- /**************************************************************************
- * auxInitPosition() - Initialize window attribute variables
- **************************************************************************/
-
- void
- auxInitPosition( GLint x, GLint y, GLsizei width, GLsizei height )
- {
- win.x = x;
- win.y = y;
-
- if ( width <= ZERO )
- auxFatalError( "Window width <= 0" );
-
- if ( height <= ZERO )
- auxFatalError( "Window height <= 0" );
-
- win.width = width;
- win.height = height;
-
- initPosition = GL_TRUE;
- }
-
-
- /**************************************************************************
- * auxInitDisplayMode() - Initialize window modes
- **************************************************************************/
-
- GLvoid
- auxInitDisplayMode( GLuint mode )
- {
- GLint pos = ZERO;
-
- if ( ((mode & AUX_SINGLE) && (mode & AUX_DOUBLE)) ||
- ((mode & AUX_INDEX) && (mode & AUX_RGBA)) )
- auxFatalError( "Contradicting window modes" );
-
- if ( mode & AUX_DOUBLE )
- attribList[pos++] = GLX_DOUBLEBUFFER;
-
- if ( mode & AUX_RGBA ) {
- attribList[pos++] = GLX_RGBA;
- /* setting R,G,and B _SIZEs to 1 should choose largest avail. */
- attribList[pos++] = GLX_RED_SIZE;
- attribList[pos++] = 1;
- attribList[pos++] = GLX_GREEN_SIZE;
- attribList[pos++] = 1;
- attribList[pos++] = GLX_BLUE_SIZE;
- attribList[pos++] = 1;
-
- if ( mode & AUX_ALPHA ) {
- attribList[pos++] = GLX_ALPHA_SIZE;
- attribList[pos++] = 1;
- }
-
- colormapMode = RGB_MODE;
- } else
- colormapMode = INDEX_MODE;
-
-
- if ( mode & AUX_DEPTH ) {
- attribList[pos++] = GLX_DEPTH_SIZE;
- attribList[pos++] = 1;
- }
-
- if ( mode & AUX_STENCIL ) {
- attribList[pos++] = GLX_STENCIL_SIZE;
- attribList[pos++] = 1;
- }
-
- if ( mode & AUX_ACCUM ) {
- attribList[pos++] = GLX_ACCUM_RED_SIZE;
- attribList[pos++] = 1;
- attribList[pos++] = GLX_ACCUM_GREEN_SIZE;
- attribList[pos++] = 1;
- attribList[pos++] = GLX_ACCUM_BLUE_SIZE;
- attribList[pos++] = 1;
-
- if ( mode & AUX_ALPHA ) {
- attribList[pos++] = GLX_ACCUM_ALPHA_SIZE;
- attribList[pos++] = 1;
- }
- }
-
- if ( mode & AUX_AUX )
- auxNote( "AUX_AUX option not supported" );
-
- attribList[pos] = None;
-
- #ifdef DEBUG
- auxPrintAttributeList( attribList );
- #endif
- }
-
-
- /**************************************************************************
- * WaitForMapNotify() - delay function to make sure window is on screen
- **************************************************************************/
-
- static Bool
- WaitForMapNotify( Display *display, XEvent *event, XPointer data )
- {
- Window glxWindow = (Window) data;
-
- if ( event->type == MapNotify && event->xmap.window == glxWindow )
- return GL_TRUE;
- else
- return GL_FALSE;
- }
-
-
- /**************************************************************************
- * auxInitWindow - initialize window
- **************************************************************************/
-
- void
- auxInitWindow( char *title )
- {
- unsigned int borderWidth = ZERO;
- unsigned long configMasks;
- auxWindow *new;
- XEvent event;
- XSizeHints sizeHints;
- XSetWindowAttributes windowAttrib;
- XTextProperty windowProp;
-
- static GLint windowId = ZERO;
- int i;
- GLboolean tryNewVisual = GL_FALSE;
-
- if ( !auxInitCalled )
- auxInit();
-
- new = (auxWindow *) malloc( sizeof( auxWindow ) );
-
- new->id = windowId;
- if ( initPosition ) {
- new->x = win.x;
- new->y = win.y;
- new->width = win.width;
- new->height = win.height;
- } else {
- new->x = 0;
- new->y = 0;
- new->width = 100;
- new->height = 100;
- }
- new->prev = (auxWindow *) NULL;
- new->next = (auxWindow *) NULL;
-
- if ( !auxState.head ) {
- auxState.head = new;
- auxState.tail = new;
- } else {
- auxState.tail->next = new;
- auxState.tail = new;
- }
-
- auxState.current = new;
- windowId++;
-
- new->glxVisual = glXChooseVisual( auxState.display, auxState.screenNum,
- attribList );
-
- if ( new->glxVisual == (XVisualInfo *) NULL )
- {
- for ( i = 0; (i < MAX_ATTRIB) && (attribList[i] != None); i++ )
- {
- switch( attribList[i] ) {
- case GLX_ALPHA_SIZE :
- i++;
- fprintf( stderr,
- "Unable to find visual with GLX_ALPHA_SIZE = %d, trying 0\n",
- attribList[i] );
- attribList[i] = 0;
- tryNewVisual = GL_TRUE;
- break;
-
- case GLX_ACCUM_ALPHA_SIZE :
- i++;
- fprintf( stderr,
- "Unable to find visual with GLX_ACCUM_ALPHA_SIZE = %d, trying 0\n",
- attribList[i] );
- attribList[i] = 0;
- tryNewVisual = GL_TRUE;
- break;
-
- default:
- break;
- }
- /* if attribList is changed, try a new visual, */
- /* else check next attrib */
- if ( tryNewVisual )
- {
- new->glxVisual = glXChooseVisual( auxState.display,
- auxState.screenNum, attribList );
- tryNewVisual = GL_FALSE;
-
- /* if a valid visual is found, break out */
- if ( new->glxVisual != (XVisualInfo *) NULL )
- {
- break;
- }
- }
- }
- /* if still no valid visual, print fatal error and exit */
- if ( new->glxVisual == (XVisualInfo *) NULL )
- {
- auxFatalError( "Unable to find visual" );
- }
- }
-
- #ifdef DEBUG
- auxPrintVisualInfo( new->glxVisual );
- #endif
-
- if ( colormapMode == RGB_MODE ) {
- if ( !auxState.rgbColormap )
- auxState.rgbColormap = XCreateColormap( auxState.display,
- auxState.rootWindow, new->glxVisual->visual, AllocNone );
-
- new->colormap = auxState.rgbColormap;
- windowAttrib.border_pixel = 0x0;
- } else {
- if ( !auxState.indexColormap )
- auxState.indexColormap = XCreateColormap( auxState.display,
- auxState.rootWindow, new->glxVisual->visual, AllocAll );
-
- new->colormap = auxState.indexColormap;
- windowAttrib.border_pixel = BlackPixel( auxState.display,
- auxState.screenNum );
- }
- colormapMode = NOT_SET;
-
- windowAttrib.colormap = new->colormap;
- windowAttrib.event_mask = ExposureMask | StructureNotifyMask |
- KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
- ButtonMotionMask | PointerMotionMask;
-
- configMasks = CWBorderPixel | CWColormap | CWEventMask;
-
- new->glxWindow = XCreateWindow( auxState.display, auxState.rootWindow,
- new->x, new->y, new->width, new->height, borderWidth, new->glxVisual->depth,
- InputOutput, new->glxVisual->visual, configMasks, &windowAttrib );
-
- if ( initPosition ) {
- sizeHints.flags = USPosition | PSize;
- XSetWMNormalHints( auxState.display, new->glxWindow, &sizeHints );
- }
-
- if ( keepAspect ) {
- sizeHints.flags = PAspect;
- sizeHints.min_aspect.x = sizeHints.max_aspect.x = win.aspectWidth;
- sizeHints.min_aspect.y = sizeHints.max_aspect.y = win.aspectHeight;
- XSetStandardProperties( auxState.display, new->glxWindow, title, title, None, NULL, 0, &sizeHints );
- }
-
- XSetWMColormapWindows( auxState.display, new->glxWindow, &new->glxWindow,
- ONE );
- XStringListToTextProperty( &title, ONE, &windowProp );
- XSetWMName( auxState.display, new->glxWindow, &windowProp );
- XSetWMIconName( auxState.display, new->glxWindow, &windowProp );
-
- XMapWindow( auxState.display, new->glxWindow );
- XIfEvent( auxState.display, &event, WaitForMapNotify,
- (XPointer) new->glxWindow );
-
- new->glxContext = glXCreateContext( auxState.display, new->glxVisual, NULL,
- GL_TRUE );
-
- auxWinSet( MINUS_ONE );
-
- initPosition = GL_FALSE;
- keepAspect = GL_FALSE;
- }
-